﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Renren.Helpers;
using Renren.Models;
using Renren.ViewModels;

namespace Renren.Services
{
    public static class PhotoService
    {
        public static void GetPhoto(NewsModel news, PhotosViewModel photosViewModel, int page = 1, int count = 20)
        {
            var parameters = new Dictionary<string, string>();
            parameters.Add("method", "photos.get");
            if (news.MediaList != null && news.MediaList.Count > 0)
            {
                if (news.FeedType == FeedType.UploadPhoto)
                {
                    parameters.Add("uid", news.ActorId.ToString());
                    parameters.Add("aid", news.SourceId.ToString());
                    parameters.Add("page", page.ToString());
                    parameters.Add("count", count.ToString());
                }
                else if (news.FeedType == FeedType.SharePhoto)
                {
                    parameters.Add("uid", news.MediaList[0].OwnerId);
                    parameters.Add("pids", news.MediaList[0].MediaId);
                }
            }

            Service.SendRequest(
                parameters,
                result =>
                {
                    photosViewModel.PhotoList.Clear();
                    var root = XElement.Parse(result);
                    foreach (var element in root.Elements())
                    {
                        var photo = new PhotoModel();
                        photo.PhotoId = element.GetValue<long>("pid");
                        photo.AlbumId = element.GetValue<long>("aid");
                        photo.UserId = element.GetValue<long>("uid");
                        photo.UrlTiny = element.GetValue("url_tiny");
                        photo.UrlHead = element.GetValue("url_head");
                        photo.UrlLarge = element.GetValue("url_large");
                        photo.Caption = element.GetValue("caption");
                        photo.Time = element.GetValue<DateTime>("time");
                        photo.ViewCount = element.GetValue<long>("view_count");
                        photo.CommentCount = element.GetValue<long>("comment_count");
                        photosViewModel.PhotoList.Add(photo);
                    }
                });
        }
    }
}
